package com.jyh.service.money;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.util.StringUtils;
import com.jyh.util.Time;

@Service
public class CapitalCostService extends BaseAction {
	@Autowired
	private LoanDao loanDao;

	/**
	 * 获取资金成本页面数据
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getCapitalCost(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/capitalCost");
		String range = request.getParameter("range");
		String dayTime1 = request.getParameter("dayTime1");
		String dayTime2 = request.getParameter("dayTime2");
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;
		String sql1 = "select count(1) as count from jyh_money_cost_log where 1=1 ";
		String sql = "select * from jyh_money_cost_log where 1=1 ";

		if (StringUtils.isNotNull(range) && range.equals("1")) {
			sql = sql + " and dayTime like '%" + Time.getTime() + "%'";
			sql1 = sql1 + " and dayTime like '%" + Time.getTime() + "%'";
		}
		if (StringUtils.isNotNull(range) && range.equals("7")) {
			sql = sql + " and dayTime<='" + Time.getTimeHMS()+ "' and dayTime>='" + Time.getDate(7) + "'";
			sql1 = sql1 + " and dayTime<='" + Time.getTimeHMS()+ "' and dayTime>='" + Time.getDate(7) + "'";
		}
		if (StringUtils.isNotNull(range) && range.equals("30")) {
			sql = sql + " and dayTime<='" + Time.getTimeHMS()+ "' and dayTime>='" + Time.getDate(30) + "'";
			sql1 = sql1 + " and dayTime<='" + Time.getTimeHMS()+ "' and dayTime>='" + Time.getDate(30) + "'";
		}
		if (StringUtils.isNotNull(dayTime1)) {
			sql = sql + " and dayTime>='" + dayTime1 + "'";
			sql1 = sql1 + " and dayTime>='" + dayTime1 + "'";
		}
		if (StringUtils.isNotNull(dayTime2)) {
			sql = sql + " and dayTime<='" + dayTime2 + "'";
			sql1 = sql1 + " and dayTime<='" + dayTime2 + "'";
		}
		
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		if(count<=lineSize){
			limitSql = " limit 0," + lineSize;
		}
		sql = sql + " order by dayTime desc" + limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		mv.addObject("list", list);
		//将传递过来的参数回显
		mv.addObject("range", range);
		mv.addObject("dayTime1", dayTime1);
		mv.addObject("dayTime2", dayTime2);
		return mv;
	}

	

}
